package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016101500694866";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDK2B5gOEU0VFetm3w5ZWtJ+bUIRn4x7pZEgoiNeQ0jGsta6Qee24icXDknFel5S8WALqeQwjiYc12O4x3ld2RIicrvEBI5u7pYCa8eUfO2eS10Wu9wRyLRg4r0JkQBfsT2e2LeMqTedaFJ3OwZW1PkxGL4kfX43D8O5/44AbI6hcwuFx7m5TlVS4uT8viWeVIUHW0Dc40KB71krFFatZTx/8D2HUsAPt7OApocPnj2yY9dSEnfXuWRS7b9NCAPlO6g9tHe+q5NbslsACSMcjH5WcqwmqlyCzhdm5k0CG2yZym3jgo403ud5Q0GvYMWF2tUCKdtuB0RSLNmCMMZNlLJAgMBAAECggEAS5kaURAHssYQW1KMjLOgtdRLxQbIt4FNHcFsdRV9cOzmhz48Kdd15Zh1cHy5JAy6UdiReOD5yM+OOdepZ8EEbgh0yn453Kx7eZ46VtmCNCqbZzcQ786DSoR1ACNWyxvc+mPn6BoF2icK8oVUtOqVrLgIjs2PV612Du8ljugK7cYPfarpZebQXJ0l4gxmtK60NZIREEOsfYA1LjSqJMSdS1B3clRV4VUZSfHJf103QkUvKPvXittgHOWGEvD0YCUlaTjFDOB56Oyl2mtXGtykSABdGNdDTXoeKWxzIbDYktveTa3rqBE2ZH076+7Nzd1uTegV6vbJ8AxMFYpJdL/GUQKBgQD2hJVf++NsVPmc5oSxDp0P6njlgV2eytQ3OjLhV54ynJv/wIzoMxT72tDL4LNmM4XB7mSKhUyfYW1AS8wqffUQj1nlGxFPY0t+T9zR+ud/HKbDGwxrp3x66mBm0SgWDmcYCc8A2BGprQ6us0S48xYLJWPKrePnKm5a1p10REr4lQKBgQDSpX0Eo9agY6zZsTuAUPTOZXyW/uWdkoIH1VPsEmgefYje5MyY8zh4Ozg50bKvaFuhxGzzFuuJVhjlWOH5+vbVYErdomM8gEX05Fhh9ozrTm7ex35/T6RITC7QF+UhIbdMHYjwugPl+9NQbSnuzY+vkp5oZgUQVt8/68ORq0lAZQKBgBL/1u9l33pK9k4EajenM621WYn+tXN2mHKFNsStVxD5hwBoG7QNs9FRZQRBljILSITQ1cjjSyuBe8UX0m139bpIAhqpQmVG9tuOViuM1eizEZs563QknXBvvexOtHM2uvVcVCrxfDMCln4abPKAMbO8Nvu4+Taoy4aOweXjGZAdAoGAZ+kcz9ImiP5CgSzNplHJQZuMp0vcZyujm0uf13+I/yl8AEArsXOhsqW76cHI/IBlW1pzSENRM3FWXKTI7LDhXIbgx45Yl7rLR6/w7XpbL6SESr9sZk8aQlPab+qpq/te2VxkdiO80pG0PyNqoROkQr74B5Xz/4hrHrQlyFEPlIUCgYAGhmay1HDDCYomzWU1ju55G2yPex2tmj/72EBCMZ0IUXOVCxqB0YRYSyTLuQ/irog+Dnyb+8lvpl7E/Si8fCYva3SLh2GSsvzu1Db0FOPET3UD02ElP72RZRMYBsrU3a645ZwOEcyhpSiMoygGAkqxwAlfL9/+qJdY9rLuh6dAxw==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAytgeYDhFNFRXrZt8OWVrSfm1CEZ+Me6WRIKIjXkNIxrLWukHntuInFw5JxXpeUvFgC6nkMI4mHNdjuMd5XdkSInK7xASObu6WAmvHlHztnktdFrvcEci0YOK9CZEAX7E9nti3jKk3nWhSdzsGVtT5MRi+JH1+Nw/Duf+OAGyOoXMLhce5uU5VUuLk/L4lnlSFB1tA3ONCge9ZKxRWrWU8f/A9h1LAD7ezgKaHD549smPXUhJ317lkUu2/TQgD5TuoPbR3vquTW7JbAAkjHIx+VnKsJqpcgs4XZuZNAhtsmcpt44KONN7neUNBr2DFhdrVAinbbgdEUizZgjDGTZSyQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://mw6by39mgx.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "3m";
    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        		+ "\"total_amount\":\""+ total_amount +"\","
        		+ "\"subject\":\""+ subject +"\","
        		+ "\"body\":\""+ body +"\","
        		+ "\"timeout_express\":\""+timeout+"\","
        		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
